

<?php 
require_once  __DIR__ . '/includes/authenticate.php';

if (isset($_POST['playerID'])) {
	
	include_once './db_bb_connect.php';

	$playerID = $_POST['playerID'];
	$season = $_POST['season'];

	$sql = "SELECT firstname, lastname, birthdate, email, phonenr, height, position, playernumber, user_key, allowed
	FROM users
	WHERE user_key = '$playerID'";

	$result = $db->query($sql);
	$row = $result->fetch_assoc();

	$user_key = $row['user_key'];
	$firstname = $row['firstname'];
	$lastname = $row['lastname'];
	$birthdate = $row['birthdate'];
	$email = $row['email'];
	$phonenr = $row['phonenr'];
	$height = $row['height'];
	$position = ucfirst($row['position']);
	$playernumberold = $row['playernumber'];
	$allowed = $row['allowed'];

	$timestampconv = strtotime($birthdate);
	$date = date("dS M Y", $timestampconv);
	$date1 = new DateTime($birthdate);
	$date3 = new DateTime("now");
	$interval = $date1->diff($date3);

	if ($allowed == 1) {
		$allowedstate = 'Yes';
	} else {
		$allowedstate = 'No';
	}

	$sql = "SELECT playernumber
	FROM teams
	WHERE playerID = '$playerID'
	AND season = '$season'";

	$result = $db->query($sql);
	$row = $result->fetch_assoc();

	$playernumber = $row['playernumber'];
	
	?>

	<style type="text/css">

	#playerinfo{
			font-size:14px;
	}

	.editprofileentry, .deleteprofileentry, .editgeneralprofile, .editpointofattention, .deletepointofattention {
		opacity:0.8;
	}

	.editprofileentry:hover, .deleteprofileentry:hover, .editgeneralprofile:hover, .editpointofattention:hover, .deletepointofattention:hover {
		opacity:1.0;
	}

	#editentryform{
		position:absolute;
		left:0px;
		top:10px;
	}

	#deleteentryform{
		position:absolute;
		left:35px;
		top:10px;
	}

	#editgeneralprofileform{
		//position:absolute;
		//top:20px;
		//right:150px;
		float:right;
	}

	</style>

<br><br>


<div class="row">
<div class="col-md-12">

<button onclick="myFunction()" class="hidden-print">Print this Profile</button>
<br>
<hr/>
<h3>Profile of <?= $firstname; ?> <?= $lastname; ?> </h3>
<?= $date3->format('dS-M-Y'); ?>
</div>
</div>


<div class="row" id="playerinfo">
<hr/>

<h4>General Player Info <form id=\"editgeneralprofileform\" class="hidden-print"> 
    <input type='hidden' name='user_key' value='<?= $user_key; ?>'/>
    <input type='hidden' name='season' value='<?= $season; ?>'/>
	<input type='image' class='editgeneralprofile float-right hidden-print'  src='./images/edit_button.png' width=20px height=20px/> 
</form> </h4>

<div class="col-lg-3 col-md-4">



<img src="./images/<?= strtolower($firstname); ?>_<?= strtolower($lastname); ?>.jpeg" width=150px class="img-circle"/> <br/>

</div>
<div class="col-lg-5 col-md-4"> 
<strong> Name: </strong> <?= $firstname; ?> <?= $lastname; ?> <br/>
<strong> Age: </strong><?php echo "$date ($interval->y years old)"; ?><br/>
<strong> Height: </strong> <?= $height; ?> cm<br/>
<strong> Position: </strong> <?= $position; ?><br/>
<strong> Number: </strong><?= $playernumber; ?>
</div>
<div class="col-lg-4 col-md-4"> 
<strong> Tel nr: </strong><?= $phonenr; ?><br/>
<strong> Email:</strong><?= $email; ?><br/>
<strong> Years played: </strong><br/>
<div class="hidden-print"><strong> Login allowed: </strong> <?= $allowedstate; ?></div>

</div>
</div>


<br>
<hr/>



<div class="row">
<div class="col-md-12"> 
<h4>Game Stats</h4>
<?php 

$sql3 = "SELECT DISTINCT season 
FROM stats 
WHERE playerID = '$user_key'";

$result3=$db->query($sql3);


while ($row3=$result3->fetch_assoc()) {
	$seasonstats = $row3['season'];
	echo "
	<form class='pull-left hidden-print'>
	&nbsp;
 	<input type=\"hidden\" name=\"playerID\" value=\"$playerID\"/>
 	<input type=\"hidden\" name=\"season\" value=\"$seasonstats\"/>

 	<button class=\"btn btn-default cumulativeplayerstats hidden-print\" type=\"submit\"> $seasonstats </button> 
 	</form> 
	";
}

?>

<div id="playerprofilestats"></div>

</div>
</div>

<div class="row">
<div class="col-md-12"> 
<h4>Training Stats</h4>

<?php 

$sql3 = "SELECT DISTINCT season 
FROM trainingft 
WHERE playerID = '$user_key'";

$result3=$db->query($sql3);


while ($row3=$result3->fetch_assoc()) {
	$seasonstats = $row3['season'];
	echo "
	<form class='pull-left hidden-print'>
	&nbsp;
 	<input type=\"hidden\" name=\"playerID\" value=\"$playerID\"/>
 	<input type=\"hidden\" name=\"season\" value=\"$seasonstats\"/>

 	<button class=\"btn btn-default cumulativeplayertrainingstats hidden-print\" type=\"submit\"> $seasonstats </button> 
 	</form> 
	";
}

?>
<div id="playerprofiletrainingstats"></div>
</div>
</div>

<hr/>
<div class="row">
<div class="col-md-12" id="ppoints"> 
<h4>Personal Points of Attention  </h4>
	<form>
		<input type="hidden" name="playerID" value="<?= $user_key ?>"/> 
		<button class="btn btn-success hidden-print pull-left playerpointsofattentionadd" type="submit">Add</button>
	</form>  
	<form> 
	<input type="hidden" name="playerID" value="<?= $user_key ?>"/>
 	<button class="btn btn-default hidden-print playerpointsofattentionshowall pull-right" type="submit">Show All</button> 
 </form> 

<br><br><br>
<div class="table-responsive">
			<table class="table table-hover table-striped">
			    <thead>
			      <th>Date</th>
			      <th>Category</th>
			      <th>Point</th>
			      <th class="hidden-print">Visibility</th>
			      <th class="hidden-print">Edit</th>
			    </thead>

			    	<?php

			    	$sql = "SELECT * 
			    	FROM attentionpointsplayers
			    	WHERE playerID = '$user_key'
			    	AND date > (NOW() - INTERVAL 1 MONTH)
			    	ORDER BY date DESC";

			    	$result=$db->query($sql);

			    	while ($row=$result->fetch_assoc()) {

			    		$category = $row['category'];
			    		$text = $row['text'];
			    		$visibility = $row['visibility'];
			    		$timestamp = $row['date'];
			    		$timestampconv = strtotime($timestamp);
						$date = date("d-m-y", $timestampconv);
			    		$ID = $row['ID'];

			    		echo "
			    		<tr>
			    		<td> $date </td>
			    		<td> $category </td>
			    		<td> $text </td>
			    		<td class=\"hidden-print\"> $visibility </td>
			    		<td class=\"hidden-print\"> 
			    		<form id=\"\" class=\"pull-left\"> 
						<input type='hidden' name='ID' value='$ID'/>
						<input type='image' class='editpointofattention'  src='./images/edit_button.png' width=20px height=20px/> 
						</form>
						<form id=\"\" class=\"pull-left\"> 
						<input type='hidden' name='ID' value='$ID'/>
						<input type='image' class='deletepointofattention' src='./images/delete_button.png' width=20px height=20px/>
						</form>
						</td>
			    		</tr>
			    		";

			    	}
			    	?>
			    <tbody>
			    </tbody>
			    </table>
			</div>
</div>




</div>
<hr/>


<?php

function getProfileCatContent($category, $playerID) {

include './db_bb_connect.php';

$sql = "SELECT * FROM profiles
WHERE playerID = '$playerID'
AND category = '$category'
ORDER BY ID DESC";

$result = $db->query($sql);

while ($row = $result->fetch_assoc()) {
	$noteID = $row['ID'];
	$content = nl2br($row['content']);
	$timestamp = $row['date'];
	$timestampconv = strtotime($timestamp);
	$date = date("d-m-y", $timestampconv);

	echo "<li class=\"list-group-item\"> 
	<div class=\"container-fluid\"> 
	<div class=\"row\"> 
	<div class=\"col-xs-10 col-sm-10 col-md-10 col-lg-9\"> 
	<strong> $date </strong> | $content  
	</div>	

	<div class='col-xs-2 col-sm-2 col-md-2 col-lg-3 hidden-print'> 
	<br>
	<form id=\"editentryform\"> 
	<input type='hidden' name='noteid' value='$noteID'/>
	<input type='image' class='editprofileentry'  src='./images/edit_button.png' width=25px height=25px/> 
	</form>
	&nbsp; &nbsp;
	<form id=\"deleteentryform\"> 
	<input type='hidden' name='noteid' value='$noteID'/>
	<input type='image' class='deleteprofileentry' src='./images/delete_button.png' width=25px height=25px/>
	</form>
	</div>
	</div>
	</div>
	<hr/>
	</li>";
}
};


$sql2 = "SELECT * FROM profiles
WHERE playerID = '$playerID'
ORDER BY ID DESC";

$result2 = $db->query($sql2);

while ($row2 = $result2->fetch_assoc()) {

	$category = $row2['category'];
	$content = $row2['content'];

}


?>
<div class="panel-group">
    
	    <div class="panel panel-default">
	      <div class="panel-heading"><strong>Personal</strong></div>
	      <div class="panel-body"> 
	      <ul class="list-group">

<?php

getProfileCatContent('personal',$playerID);

?>
			</ul> 

		</div>
    </div>
    <br>

    <div class="panel panel-primary">
      <div class="panel-heading"><strong>Fundamentals</strong></div>
      <div class="panel-body">
		<ul class="list-group">
<?php

getProfileCatContent('fundamentals',$playerID);

?>
			</ul> 
      </div>
    </div>
     <br>

    <div class="panel panel-success">
      <div class="panel-heading"><strong>Training</strong></div>
      <div class="panel-body">
      	<ul class="list-group">
<?php

getProfileCatContent('training',$playerID);


?>
			</ul> 
      </div>
    </div>
     <br>

    <div class="panel panel-success">
      <div class="panel-heading"><strong>Game</strong></div>
      <div class="panel-body">
      	<ul class="list-group">
<?php

getProfileCatContent('game',$playerID);


?>
			</ul> 
      </div>
    </div>
     <br>

    <div class="panel panel-danger">
      <div class="panel-heading"><strong>Injuries</strong></div>
      <div class="panel-body">

      <ul class="list-group">
<?php


getProfileCatContent('injuries',$playerID);


?>
			</ul> 
      </div>
    </div>
</div>

<?php } ?>

<div id="editcontent"></div>

<script type="text/javascript">

$(".editprofileentry").on("click", function(e) {
		postData = $(this.form).serialize();
		$.ajax({
 			type: "POST",
  			url: "./playerprofile_editnote.php",
  			data: postData,
  			success: function(data) { 				
 				$("#playerprofilecontent").html(data);
  			},
  			error: function (xhr, status, error) {
  				alert(xhr.responseText);
  			}
  		}); 
		e.preventDefault();
});

$(".editgeneralprofile").on("click", function(e) {
		postData = $(this.form).serialize();
		$.ajax({
 			type: "POST",
  			url: "./playerprofile_editgeneral.php",
  			data: postData,
  			success: function(data) { 
 				$("#playerprofilecontent").html(data);
  			},
  			error: function (xhr, status, error) {
  				alert(xhr.responseText);
  			}
  		}); 
		e.preventDefault();
});


$(".deleteprofileentry").on("click", function(e) {
		postData = $(this.form).serialize();
		$.ajax({
 			type: "POST",
  			url: "./includes/handlers/playerprofile_deletenote.php",
  			data: postData,
  			success: function(data) { 				
 				$(this).parent().parent().fadeOut(3000);
				$(this).parent().parent().html("Entry Deleted");
  			},
  			error: function (xhr, status, error) {
  				alert(xhr.responseText);
  				$(this).parent().parent().html("Entry Deletion Failed");
  			}
  		}); 
		e.preventDefault();
		$(this).parent().parent().parent().fadeOut(3000);
		$(this).parent().parent().parent().html("Entry Deleted");

});

$(".cumulativeplayerstats").on("click", function(e) {
		postData = $(this.form).serialize();
		$.ajax({
 			type: "POST",
  			url: "./playerprofile_cumulative_playergamestats.php",
  			data: postData,
  			success: function(data) { 
 				$("#playerprofilestats").html(data);				
  			},
  			error: function (xhr, status, error) {
  				alert(xhr.responseText);
  				
  			}
  		}); 
		e.preventDefault();		
});

$(".cumulativeplayertrainingstats").on("click", function(e) {
		postData = $(this.form).serialize();
		$.ajax({
 			type: "POST",
  			url: "./playerprofile_cumulative_playertrainingstats.php",
  			data: postData,
  			success: function(data) { 
 				$("#playerprofiletrainingstats").html(data);				
  			},
  			error: function (xhr, status, error) {
  				alert(xhr.responseText);
  				
  			}
  		}); 
		e.preventDefault();		
});

$(".playerpointsofattentionshowall").on("click", function(e) {
		postData = $(this.form).serialize();
		$.ajax({
 			type: "POST",
  			url: "./playerpointsofattention.php",
  			data: postData,
  			success: function(data) { 
 				$("#ppoints").html(data);				
  			},
  			error: function (xhr, status, error) {
  				alert(xhr.responseText);
  			}
  		}); 
		e.preventDefault();		
});

$(".playerpointsofattentionadd").on("click", function(e) {
		postData = $(this.form).serialize();
		$.ajax({
 			type: "POST",
  			url: "./playerpointsofattention_add.php",
  			data: postData,
  			success: function(data) { 
 				$("#fullplayerprofile").html(data);				
  			},
  			error: function (xhr, status, error) {
  				alert(xhr.responseText);
  			}
  		}); 
		e.preventDefault();		
});




$(".editpointofattention").on("click", function(e) {
		postData = $(this.form).serialize();
		$.ajax({
 			type: "POST",
  			url: "./playerpointsofattention_edit.php",
  			data: postData,
  			success: function(data) { 
 				$("#fullplayerprofile").html(data);				
  			},
  			error: function (xhr, status, error) {
  				alert(xhr.responseText);
  			}
  		}); 
		e.preventDefault();		
});

$(".deletepointofattention").on("click", function(e) {
		postData = $(this.form).serialize();
		$.ajax({
 			type: "POST",
  			url: "./includes/handlers/playerpointsofattention_delete_handler.php",
  			data: postData,
  			success: function(data) { 
 				$("#fullplayerprofile").html(data);				
  			},
  			error: function (xhr, status, error) {
  				alert(xhr.responseText);
  			}
  		}); 
		e.preventDefault();		
});





</script>
